package com.ekingstar.framework.security;

import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ekingstar.framework.domain.OnlineUser;
import com.ekingstar.framework.domain.SystemLog;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.service.IOnlineUserService;
import com.ekingstar.framework.service.ISystemLogService;
import com.ekingstar.framework.service.IUserService;

/**
 * 
 * @author GQ
 * 
 */
@SuppressWarnings("unchecked")
public class SessionListener implements HttpSessionListener {

	static Hashtable hashtable = new Hashtable();

	// 实现httpsessionlistener接口，完成session创建事件控制
	public void sessionCreated(HttpSessionEvent arg0) {
		HttpSession session = arg0.getSession();
		hashtable.put(session.getId(), session);
	}

	// 实现httpsessionlistener接口，完成session销毁事件控制
	public void sessionDestroyed(HttpSessionEvent event) {

		User user = UserUtil.getCurrentUser();
		IOnlineUserService onlineUserService = (IOnlineUserService) WebApplicationContextUtils
				.getWebApplicationContext(
						event.getSession().getServletContext()).getBean(
						"onlineUserService");

		OnlineUser ou = onlineUserService.getUseIdBySessionId(event
				.getSession().getId());

		String ip = onlineUserService.getIpByCurrentUser(user);

		if (user != null && ou != null) {
			// System.out.println("-------------"+ou.getTUser().getId());
			// System.out.println("-------------"+user.getId());
			if (user.getId().equals(ou.getTUser().getId())) {
				String userid = user.getId();
				SystemLog systemLog = new SystemLog();
				systemLog.setUserId(userid);
				systemLog.setTime(new Date());
				systemLog.setType("logout");
				systemLog.setModelName("系统管理");
				systemLog.setIp(ip);
				systemLog.setDescription("用户退出系统");
				ISystemLogService systemLogService = (ISystemLogService) WebApplicationContextUtils
						.getWebApplicationContext(
								event.getSession().getServletContext())
						.getBean("systemLogService");
				systemLogService.addLog(systemLog);
				onlineUserService.deleteUser(user);
			} else {
				String userid = user.getId();
				SystemLog systemLog = new SystemLog();
				systemLog.setUserId(userid);
				systemLog.setTime(new Date());
				systemLog.setType(SystemLog.LogType.kick_user.name());
				systemLog.setIp(ip);
				systemLog.setModelName("系统管理");
				systemLog.setDescription("踢出用户(用户帐号:" + ou.getUserName() + ")");
				ISystemLogService systemLogService = (ISystemLogService) WebApplicationContextUtils
						.getWebApplicationContext(
								event.getSession().getServletContext())
						.getBean("systemLogService");
				systemLogService.addLog(systemLog);
				user = new User();
				user.setName(ou.getUserName());
				onlineUserService.deleteUser(user);
			}			
			
			IUserService userService = (IUserService) WebApplicationContextUtils
			.getWebApplicationContext(
					event.getSession().getServletContext()).getBean(
					"userService");
			user.setLastlogoutTime(new Date());
			userService.saveUser(user);
						
		}
		
		HttpSession session = event.getSession();
		hashtable.remove(session.getId());
	}

	// 返回全部session对象集合

	static public Iterator getset() {
		return hashtable.values().iterator();
	}

	// 依据session id返回指定的session对象
	static public HttpSession getSession(String sessionid) {
		return (HttpSession) hashtable.get(sessionid);

	}

}
